package com.vasthu.core.dao;

import com.vasthu.api.dao.RoleDAO;
import com.vasthu.api.domain.Role;
import java.util.*;
import java.sql.*;

public class RoleDAOImpl implements RoleDAO {

	public boolean insertRole(Connection con, Role role) {
		if (role == null)
			return false;
			
		PreparedStatement pst = null;
		
		try {
			pst = con.prepareStatement("INSERT INTO Role(role_name, role_desc, status) VALUES(?, ?, 1)");
			
            pst.setString(1, role.getRoleName());
            pst.setString(2, role.getRoleDescription());

            pst.executeUpdate();
			
		} catch (Exception ex) {
			System.out.println("ERROR : RoleDAOImpl->insertRole()");
		}
		
		return true;
	}

	public Role getRoleById(Connection con, long roleId) {
		PreparedStatement pst = null;
		ResultSet rs = null;
		Role role = new Role();
		
		try {
			pst = con.prepareStatement("SELECT * FROM Role WHERE role_id = ? AND status = 1");
			pst.setLong(1, roleId);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				role.setRoleId( rs.getInt("role_id") );
				role.setRoleName( rs.getString("role_name") );
				role.setRoleDescription( rs.getString("role_desc") );
				break;
            }
			
		} catch (Exception ex) {
			System.out.println("ERROR : RoleDAOImpl->deleteRoleById()");
		}
		
		return role;
	}
	
	public Role getRoleByName(Connection con, String roleName) {
		PreparedStatement pst = null;
		ResultSet rs = null;
		Role role = new Role();
		
		try {
			pst = con.prepareStatement("SELECT * FROM Role WHERE role_name = ? AND status = 1");
			pst.setString(1, roleName);
			rs = pst.executeQuery();
			
			while (rs.next()) {
				role.setRoleId( rs.getInt("role_id") );
				role.setRoleName( rs.getString("role_name") );
				role.setRoleDescription( rs.getString("role_desc") );
				break;
            }
			
		} catch (Exception ex) {
			System.out.println("ERROR : RoleDAOImpl->deleteRoleByName()");
		}
		
		return role;
	}
	
	public boolean deleteRoleById(Connection con, long roleId) {
		PreparedStatement pst = null;

		try {
			pst = con.prepareStatement("DELETE FROM Role WHERE role_id = ?");
			pst.setLong(1, roleId);
			pst.executeUpdate();
		} catch (Exception ex) {
			System.out.println("ERROR : RoleDAOImpl->deleteRoleById()");
		}
		
		return true;
	}
    
    public List<Role> getAllRoles(Connection con) {
		List<Role> roleList = new ArrayList<Role>();
		PreparedStatement pst = null;
		ResultSet rs = null;
		
		try {
            pst = con.prepareStatement("SELECT * FROM Role WHERE status = 1");
            rs = pst.executeQuery();

			while (rs.next()) {
				Role role = new Role();
				
				role.setRoleId( rs.getInt("role_id") );
				role.setRoleName( rs.getString("role_name") );
				role.setRoleDescription( rs.getString("role_desc") );
				
				roleList.add(role);
            }

        } catch (SQLException ex) {
            System.out.println("ERROR : UserDAOImpl->getAllUsers()");
        }
		
		return roleList;
	}
}